<template>
  <div class="main1">
    <div class="login_form">
      <h2>网易考拉后台管理注册系统</h2>
      <el-form ref="form" :model="ruleForm" label-width="80px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()" style="float:right;">注册</el-button>
          <el-button @click="gotoReg" style="float:right;margin-right:5px">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import usersApi from "@/api/usersApi"; //引入接口
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    gotoReg() {
      this.$router.push("/login");
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            let p = await usersApi.reg(
              this.ruleForm.username,
              this.ruleForm.password
            );
            if (p.data.flag) {
              this.$message({
                message: "注册成功",
                type: "success"
              });
              this.$router.push({
                path: "/login"
              });
            } else {
              this.$message({
                message: "注册失败",
                type: "error"
              });
            }
          } catch (err) {
            console.log(err),
              this.$message({
                message: "服务器异常",
                type: "error"
              });
          }
        } else {
        }
      });
    }
  }
};
</script>

<style scoped>
.main1 {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/register.jpg");
  overflow: hidden;
}
h2 {
  margin-bottom: 20px;
}
.login_form {
  width: 350px;
  /* 上下间隙150px, 左右自动 */
  margin: 160px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  padding-right: 60px;
  border-radius: 20px;
}
</style>